package com.web.new12306.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.web.new12306.conn.Md5Utils;
import com.web.new12306.pojo.User;
import com.web.new12306.service.UserService;
import com.web.new12306.service.impl.UserServiceImpl;
import com.web.new12306.util.FileUtil;

/**
 * @author 苗宇
 *
 */


/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	 public RegisterServlet() {
	        super();
	        // TODO Auto-generated constructor stub
	    }
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		// 取得请求参数
		User user = new User();
		
	    // 获取客户端IP
	    	user.setLoginIp(request.getRemoteAddr());
		// 获取表单参数,将值赋给user
			user.setUsername(request.getParameter("username"));
			user.setPassword(Md5Utils.md5(request.getParameter("password")));//Md5Utils.md5("123456")
			user.setPassword2(Md5Utils.md5(request.getParameter("repassword")));
			user.setRealname(request.getParameter("realname"));
			user.setSex(request.getParameter("sex"));
			user.setProvince(request.getParameter("province"));
			user.setCity(request.getParameter("city"));
			user.setCardId(request.getParameter("cardid"));
			user.setCardType(request.getParameter("cardtype"));
			user.setType(request.getParameter("type"));
			user.setContent(request.getParameter("content"));
			String flag=request.getParameter("check");

     	// 服务器端验证
		String msg = validate(flag,user);
		//如果msg=""，，调用save方法保存用户信息
		if (msg==null) {
         UserService userService=new UserServiceImpl();
         int num= userService.saveUser(user);
         msg="恭喜您,注册成功";
         response.setContentType("text/html");
 		 out.println("<html>");
 		 out.println("<head>");
 		 out.println("<title>Hello</title>");
 		 out.println("</head>");
 		 out.println("<body>");
 		 out.println("<p>" + msg + "</p>");
 		 out.println("<p>已成功插入" + num + "条信息</p>");
 		 out.println("<p><a href=\"userLogin.jsp\">退出，跳转到登录界面</a></p>");
 //		 out.println("<p><a href=\"http://localhost:8080/new12306/userLogin.jsp\">退出，跳转到登录界面</a></p>");
 		 out.println("</body>");
 		 out.println("</html>");
 		 out.close();
		}else {
			request.setAttribute("msg", msg);
			request.getRequestDispatcher("/register.jsp").forward(request,
					response);
		}
	}

	private String validate(String flag,User user) {
		String errorMsg = null;
		if (!user.getPassword().equals(user.getPassword2())) {
			errorMsg = "两次密码不相等";
		} else if (user.getRealname()=="") {
			errorMsg = "请输入真实姓名";
		} else if (user.getCardId()=="") {
			errorMsg = "请输入证件号";
		} else if(!user.getCardId().matches("\\d{18}$|^\\d{17}[xX]")) {
			errorMsg = "证件号格式输入错误";
		}else if(flag==null) {
			errorMsg = "未同意协议";
		}
		return errorMsg;
	}

	

}
